﻿using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.PageActionManager;

namespace ZBJF.Warranty.BLL
{
    public partial class RoleToPageToActionService
    {
        public IQueryable<RoleToPageToAction> GetPagingList(RoleToPageToActionModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<RoleToPageToAction>();
            if (model.RoleID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.RoleID == model.RoleID);
            }
            if (model.BelongApplicationType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.PageToAction.PageMenu.BelongApplicationType == model.BelongApplicationType);
            }
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere = expressionWhere.And(o => (o.PageToAction.PageMenu.PageName + SqlFunctions.Space(1) + o.PageToAction.PageMenu.PageUrl).Contains(model.Keyword));
            }
            if (model.ActionID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.PageToAction.ActionID == model.ActionID);
            }
            if (model.PageID > 0)
            {
                List<int> pageIds = ServiceFactory.PageMenuServiceInstance.GetAllChildrenPageID(model.PageID);
                expressionWhere = expressionWhere.And(o => pageIds.Contains(o.PageToAction.PageID));
            }
            return LoadPagerEntities<RoleToPageToAction>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}